package scau.jump.securitytrace.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import scau.jump.securitytrace.init.Init;
import scau.jump.securitytrace.popupwindow.RoleWindow;

import com.securitytrace.model.Role;

public class UpdateRoleButtonListener implements ActionListener {

	private RoleWindow roleWindow;
	private Role role;
	
	public UpdateRoleButtonListener(RoleWindow roleWindow) {
		this.roleWindow = roleWindow;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String name = roleWindow.getRoleTextField().getText();
		String desc = roleWindow.getRoleTextArea().getText();
		if(name.trim().isEmpty()) {
			JOptionPane.showMessageDialog(roleWindow, "角色名称不能为空");
		}else if(desc.trim().isEmpty()) {
			JOptionPane.showMessageDialog(roleWindow, "角色描述不能为空");
		}else {
			role.setName(name);
			role.setDescription(desc);
			try {
				updateRoleToDataBase(role);
				roleWindow.getManagerClient().setResultRole(role);
				roleWindow.dispose();
			} catch(Exception ex) {
				JOptionPane.showMessageDialog(roleWindow, "操作失败");
			}
		}
	}

	private void updateRoleToDataBase(Role role) {
		Init.getRoleService().update(role);
		System.out.println("update role ");
	}

	/***以下为get/set方法***/
	public void setRole(Role role) {
		this.role = role;
	}
}
